package com.eusun.design.state.demo1;

import java.util.Random;

public class HasQuarterState implements State {
    private final Random random = new Random(System.currentTimeMillis());
    private final GumballMachine machine;

    public HasQuarterState(GumballMachine machine) {
        this.machine = machine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("You can't insert another quarter");

    }

    @Override
    public void ejectQuarter() {
        System.out.println("Quarter returned");
        this.machine.setState(this.machine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("You turned...");
        int w = random.nextInt(10);
        if (w == 0 && machine.getCount() > 1) {
            this.machine.setState(this.machine.getWinnerState());
        } else {
            this.machine.setState(this.machine.getSoldState());
        }
    }

    @Override
    public void dispense() {
        System.out.println("No gumball dispensed");
    }
}
